package com.zyk.leetcode;

/**
 *
 * https://leetcode-cn.com/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/
 *
 * @author zhangsan
 * @date 2021/4/2 13:41
 */
public class J44 {

    /** 暴力解 */
    public static int findNthDigit(int n) {
        StringBuilder str = new StringBuilder("0");
        for (int i = 1; i <= n; i++) {
            str.append(i);
        }
        return str.charAt(n) - 48;
    }

    public static int findNthDigit1(int n) {
        int digit = 1;
        long start = 1;
        long count = 9;
        while (n > count) { // 1.
            n -= count;
            digit += 1;
            start *= 10;
            count = digit * start * 9;
        }
        long num = start + (n - 1) / digit; // 2.
        return Long.toString(num).charAt((n - 1) % digit) - '0'; // 3.
    }

    // for test
    public static void main(String[] args) {
        /*int n = 10 + (89*2+2);
//        int n = 10 + (89*2+2) + (899*3+3);
//        int n = 10 + (89*2+2) + (899*3+3) + (8999*4+4);
        *//*System.out.println(n + " : " + findNthDigit(n++));
        System.out.println(n + " : " + findNthDigit(n++));
        System.out.println(n + " : " + findNthDigit(n++));
        System.out.println(n + " : " + findNthDigit(n++));
        System.out.println(n + " : " + findNthDigit(n++));
        System.out.println(n + " : " + findNthDigit(n++));
        System.out.println(n + " : " + findNthDigit(n++));
        System.out.println(n + " : " + findNthDigit(n++));
        System.out.println(n + " : " + findNthDigit(n++));
        System.out.println(n + " : " + findNthDigit(n++));
        for (int i = 0; i <= 200; i++) {
            System.out.println(i + " : " + findNthDigit(i));
        }*//*
        for (int i = 1; i <= 600; i++) {
            System.out.println(i+ " : " + n + " : " + findNthDigit(n++));
        }*/


        for (int i = 0; i < 2000000000; i++) {
            int ans1 = findNthDigit(i);
            int ans2 = findNthDigit1(i);
            if(ans1 != ans2) {
                System.out.println("OOPS!");
                break;
            }
        }
    }

}
